<?php
//日本語
class Post extends AppModel {

	var $name = 'Post';

	//The Associations below have been created with all possible keys, those that are not needed can be removed
	var $belongsTo = array(
		'User' => array(
			'className' => 'User',
			'foreignKey' => 'user_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
	);

	var $hasMany = array(
		'Comment' => array(
			'className' => 'Comment',
			'foreignKey' => 'post_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
	);
	//バリデート
	var $validate = array(
		'user_id' => array(
			'rule' => array('isPlusNumber'),
		),
		'ip' => array(
			'rule' =>array('ip'),
		),
		'title' => array(
			VALID_NOT_EMPTY,
			'length' => array(
				'rule' =>array('between', 1, 100),
				'message' => 'タイトルは1-100文字までです',
			),
			'kaigyo' => array(
				'rule' => array('kaigyoLessThanN', 0),
				'message' => 'タイトルは改行できません',
			),
		),
		'content' => array(
			VALID_NOT_EMPTY,
			'length' => array(
				'rule' =>array('between', 1, 2000),
				'message' => '内容は1-2000文字までです',
			),
			'kaigyo' => array(
				'rule' => array('kaigyoLessThanN', 19),
				'message' => '内容は20行までです',
			),
		),
		'status' => array(
			'rule' => array('isInArray', array('active', 'disable')),
		),
		'control' => array(
			'rule' => array('isInArray', array('onlyMe', 'friend', 'friendFriend', 'all')),
		),
	);
	
	function register($userID, $title, $content, $control)
	{
		$data = array();
		$data['user_id'] = $userID;
		$data['title'] = $title;
		$data['status'] = 'active';
		$data['content'] = $content;
		$data['control'] = $control;
		$data['ip'] = $this->RHgetClientIP();
		
		return $this->save(array($this->name => $data));
	}	
	function modify($title, $content, $control)
	{
		$data = array();
		$data['ip'] = $this->RHgetClientIP();
		$data['title'] = $title;
		$data['content'] = $content;
		$data['control'] = $control;
		$data['modified'] = null;
		return $this->save(array($this->name => $data), true, array('ip', 'title', 'content', 'modified', 'control'));
	}


}
?>